/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package objective;

/**
 *
 * @author daz
 */
public class XnotInSet implements IObjective {

    private int x;
    private int[] set;
    private int setSize;
    private int violateDegrees;

    public XnotInSet(int x, int[] set) {
        this.x = x;
        this.set = set;
        this.setSize = (set == null ? 0 : set.length);
        violateDegrees = calculateViolation(set);
    }

    public void updateVector(int index, int newValue) {
        if (index == -1) {
            //change x
            x = newValue;
        } else {
            set[index] = newValue;
        }
        violateDegrees = calculateViolation(set);
    }

    private int calculateViolation(int[] vectorVar) {
        int result = 0;
        for (int i = 0; i < setSize; i++) {
            if (x == vectorVar[i]) {
                result++;
            }
        }
        return result;
    }

    @Override
    public int getAssignDelta(int index, int newValue) {
        if (index == -1) {
            //change x
            x = newValue;
        } else {
            set[index] = newValue;
        }
        return calculateViolation(set) - violateDegrees;
    }

    @Override
    public int getViolateDegrees() {
        return violateDegrees;
    }
}
